extensions [gis]
globals [f g wlist blist line-feature R-square time-series printTime agentVision numOfAgents agentSize roadTotalLength acceleration street-list streetFeatures streetdata last-node visited-list tracked-list visited-goal-list tracked-goal-list connect-list connect-rs-list ltg-list wpr-list wpr-rs-list betweeness-list betweeness-rs-list countAbnormal countAbnormal1 maxBound currLineDepth currLevel pathList degree currPath currDegree pathIDsList street_shp mylist a b c d]
breed [nodes node]
breed [destinations destination]
breed [people person]
breed [goalmen goalman]
people-own [cur-street last-street next-street cur-length junc-length junc-point last-location cur-travelTime travelTime direction speed]
goalmen-own [cur-street last-street next-street cur-length junc-length junc-point last-location cur-travelTime travelTime direction path-goal dest-street dest-x dest-y speed]

;Load a shapefile polyline data as the streetNetwork
to Setup
  ;; (for this model to work with NetLogo's new plotting features,
  ;; __clear-all-and-reset-ticks should be replaced with clear-all at
  ;; the beginning of your setup procedure and reset-ticks at the end
  ;; of the procedure.)
  __clear-all-and-reset-ticks
  ask patches [set pcolor white]
 ; gis:load-coordinate-system "data1/avignon_NaturalRoadsAxialNew.prj"
  set street_shp gis:load-dataset "data1/avignon_NaturalRoadsAxialNew.shp"
  gis:set-world-envelope (gis:envelope-union-of (gis:envelope-of street_shp))
  
  set mylist []
  set line-feature(gis:feature-list-of street_shp)
  ;add endpoints of line features into an empty list, for a line list the format list(x1,y1,x2,y2)
  foreach line-feature ; replace the shp with the list 
  [
    let p1 gis:location-of(item 0(first (gis:vertex-lists-of ?))) ;item 0 obtain the first element of a list (the first vertex of a line). item 1 the second 
    let p2 gis:location-of(item 1(first (gis:vertex-lists-of ?)))
    ifelse (length p1 != 0 ) and (length p2 != 0) 
    [
      let line (list(item 0 p1)(item 1 p1)(item 0 p2)(item 1 p2)) ; p1-->(x1,y1) item 0 p1-->x1.... 
      set mylist lput line mylist ;lput--> append every line to mylist
      ]
    []
    ]
  set streetFeatures(mylist)
 ;set streetFeatures gis:load-dataset "barnsbury.shp"
 ;let myEnvelope gis:envelope-of streetFeatures
 ;set myEnvelope (list ((item 0 myEnvelope)- 1)((item 1 myEnvelope)+ 1)((item 2 myEnvelope)- 1)((item 3 myEnvelope)+ 1)) 
 ;gis:set-world-envelope myEnvelope
 ; set streetFeatures (list (list -19.847113930873192 16.392979979993694 -15.147226751807644 -19.507363050458817) (list -20.30011510475903 14.41109984424316 -11.693092800928143 19.110987023308713) (list -19.2808624635159 5.2944512197907105 -8.012458263105724 6.823330181655407) (list -17.015856594086717 -10.334089279270636 20.016989371080385 2.576444176475694) (list -15.54360277895775 -19.224237316780172 20.30011510475903 2.972820203625801) (list -13.731598083414406 19.507363050458817 19.73386363740174 8.748585170670209) (list -5.067950632847789 17.582108061444018 -8.975085757613126 5.804077540412275) (list -8.918460610877396 7.616082235955621 -5.124575779583518 -6.823330181655407) (list -2.9161950568900687 9.711212665177612 4.1619482850761225 -8.352209143520103) (list 18.374860115744234 10.220838985799178 19.167612170044446 -0.19818801357505164) (list 5.5209518067336285 14.014723817093058 8.465459436991564 2.6330693232114224) (list -7.955833116369996 10.787090453156473 -1.8969424156469368 9.088336051084589) (list -0.19818801357505164 0.9343149211395372 19.280862463515902 5.7474523936765465))
  ;set streetFeatures (list)
  ;ReadData
  set streetdata (list)
  set street-list (list)
  set wlist (list)
  set blist (list) 
  set countAbnormal (list)
  set countAbnormal1 0
  set acceleration 0.1
  set agentSize 0.5
  set agentVision 2
  set roadTotalLength 0
  set printTime 1
  ConvertToLinks
  set time-series n-values 10 [0]
  set wpr-rs-list (list)
  set wpr-list (list)
  set connect-list (list)
  set ltg-list (list)
  set betweeness-list (list)
  set betweeness-rs-list (list)
  ; set visited-list n-values (length streetdata) [0]
  set visited-list n-values 425 [0]
  ;set tracked-list n-values (length streetdata) [0]
  set tracked-list n-values 425 [0]
  set visited-goal-list n-values (length streetdata) [0]
  set tracked-goal-list n-values (length streetdata) [0]
  set numOfAgents car-number
  set maxBound length streetdata
  SetMeasureList
  ConstructConnectivityGraph
  InitialSimu
end
;Read the wpr value from the netlogo file
to SetMeasureList
  file-open "connect.txt"
  set connect-list file-read
  file-close
  file-open "Ltg.txt"
  set ltg-list file-read
  file-close
  file-open "wpr.txt"
  set wpr-list file-read
  file-close
  file-open "betweeness.txt"
  set betweeness-list file-read
  file-close
   (foreach wpr-list betweeness-list
  [
    
      ;let list1 (list ?)
      set wlist lput ?1 wlist
      set blist lput ?2 blist
      ifelse length(blist) > 424 and length(wlist) > 424
      [stop]
      []
     ])
  
  
 
end
;Read the netlogo street network file
;to ReadData
 ;file-open "barnsbury.txt"
  ;while [not file-at-end?]
  ;[
   ; let streetEle file-read
    ;let axialline (list (item 2 streetEle) (item 3 streetEle) (item 4 streetEle) (item 5 streetEle))
    ;set streetFeatures lput axialline streetFeatures
  ;]
  ;file-close
;end 
;Generate agents and locate them on the streets
to InitialSimu
  ifelse MovingBehavior = "Random"
  [
    GenerateRandomAgents 
  ]
  [
    GeneratePurposiveAgents
  ]
end
;Convert shapefile data to link
to ConvertToLinks
  foreach streetFeatures
  [
    let street (list)
    create-nodes 1
    [
      setxy (item 0 ?)(item 1 ?)
      set size 0.5
      set shape "circle"
      set hidden? true
      set last-node self
    ]
    create-nodes 1
    [    
      setxy (item 2 ?)(item 3 ?)
      set size 0.5
      set shape "circle"
      set hidden? true
      create-link-with last-node
      [
        set color red
        set thickness 0
        set roadTotalLength roadTotalLength + link-length
      ]
      set street (list [who] of last-node who [xcor] of last-node [ycor] of last-node xcor ycor)
    ]
    set streetdata lput street streetdata    
  ]
end
;Generate Purposive agents
to GeneratePurposiveAgents
  create-goalmen numOfAgents
  [
    let streetId (random length streetdata)
    let street (item streetId streetdata)
    let x1 item 2 street
    let y1 item 3 street
    let x2 item 4 street
    let y2 item 5 street
    let r1 random-float 1.0
    let x (x1 + (x2 - x1)* r1)
    let y (y1 + (y2 - y1)* r1)
    while [any? goalmen-on patch x y]
    [
      ; set streetId (random length streetdata)
      set streetId random 425
      set street item streetId streetdata
      set x1 item 2 street
      set y1 item 3 street
      set x2 item 4 street
      set y2 item 5 street
      set r1 random-float 1.0
      set x (x1 + (x2 - x1)* r1)
      set y (y1 + (y2 - y1)* r1)
    ]
    
    let dStreetId Purposive_walker_behavior streetId
    let dstreet item dStreetId streetdata  ;destination street
    let x3 item 2 dstreet
    let y3 item 3 dstreet
    let x4 item 4 dstreet
    let y4 item 5 dstreet
    let r2 random-float 1
    let x5 (x3 + (x4 - x3)* r2)    ;destination position x
    let y5 (y3 + (y4 - y3)* r2)    ;destination position y
    
    setxy x y
    set size agentSize
    set color green
    set shape "car top"
    set speed simulation-speed
    
    set cur-street streetId
    set last-street -1
    set next-street -1
    set cur-length sqrt((xcor - x1)*(xcor - x1)+(ycor - y1)*(ycor - y1))
    set junc-length -1
    set junc-point (list)
    set last-location (list)
    set direction 0
    set dest-street dStreetId
    set dest-x x5
    set dest-y y5
    ;calculate shortest path.
    set path-goal GetPath xcor ycor cur-street x5 y5 dStreetId
    ;Set the red color for the destination street
;    ask link (item 0 (item cur-street streetdata)) (item 1 (item cur-street streetdata)) 
;    [
;      set color white
;    ] 
;    ask destinations
;    [
;      die
;    ] 
;    ask link (item 0 (item dest-street streetdata)) (item 1 (item dest-street streetdata)) 
;    [
;      set color red
;    ]  
;    hatch-destinations 1
;    [
;      setxy x5 y5
;      set size 2.5
;      set shape "flag"
;      set color blue
;    ]   
    
    let tracked item streetId tracked-goal-list
    set tracked tracked + 1
    set tracked-goal-list replace-item streetId tracked-goal-list tracked
    let visited item streetId visited-goal-list
    set visited visited + 1
    set visited-goal-list replace-item streetId visited-goal-list visited      
  ]
end
;Purposive agents behavior
to-report Purposive_walker_behavior [streetId]
  if MovingBehavior = "Purposive I"
  [
    let nStreetId (random length streetdata)
    while [nstreetId = streetId]
    [
      set nStreetId (random length streetdata)
    ]
    report nStreetId
  ]
  if MovingBehavior = "Purposive II"
  [
    let street-adjacent item streetId street-list
    set street-adjacent sublist street-adjacent 1 (length street-adjacent)
    let localStreets (list)
    let tempStreets (list)
    let globalStreets (list)
    foreach street-adjacent
    [
      let a-street-id item 0 ?
      set tempStreets lput a-street-id tempStreets
      set localStreets lput a-street-id localStreets    
    ]  
    foreach tempStreets
    [
      let b-street-adjacent item ? street-list
      set b-street-adjacent sublist b-street-adjacent 1 (length b-street-adjacent)
      foreach b-street-adjacent
      [
        let b-street-id item 0 ?
        if b-street-id != streetId
        [
          if not member? b-street-id localStreets
          [
            set localStreets lput b-street-id localStreets
          ]
        ]
      ]
    ] 
    ;set the globalStreets
    let i 0 
    while [i < (length streetdata)]
    [
      if i != streetId
      [
        if not member? i localStreets
        [
          set globalStreets lput i globalStreets
        ]
      ]
      set i i + 1
    ]
    ;80 percent of probability to choose the next street within 3 steps from the origin
    let p random-float 1              
    ifelse p < 0.8
    [
      let nId random length localStreets
      let nStreetId item nId localStreets
      report nStreetId
    ]
    [ 
      ifelse not empty? globalStreets
      [
        let nId random length globalStreets
        let nStreetId item nId globalStreets
        report nStreetId
      ]
      [
        let nId random length localStreets
        let nStreetId item nId localStreets
        report nStreetId
      ]
    ]
  ]
end
;Generate random agents
to GenerateRandomAgents
  create-people numOfAgents
  [
    let streetId (random length streetdata)
    ;let streetId random 425
    let street item streetId streetdata
    let x1 item 2 street
    let y1 item 3 street
    let x2 item 4 street
    let y2 item 5 street
    let r1 random-float 1.0
    let x (x1 + (x2 - x1)* r1)
    let y (y1 + (y2 - y1)* r1)
    while [any? people-on patch x y]
    [
      set streetId (random length streetdata)
      ;set streetId (random 425)
      set street item streetId streetdata
      set x1 item 2 street
      set y1 item 3 street
      set x2 item 4 street
      set y2 item 5 street
      set r1 random-float 1.0
      set x (x1 + (x2 - x1)* r1)
      set y (y1 + (y2 - y1)* r1)
    ]
    setxy x y
    set size agentSize
    set color blue
    set shape "car top"
    set speed simulation-speed
    set cur-street streetId
    set last-street -1
    set next-street -1
    set cur-length sqrt((xcor - x1)*(xcor - x1)+(ycor - y1)*(ycor - y1))
    set junc-length -1
    set junc-point (list)
    set last-location (list)
    set direction 0
    let tracked item streetId tracked-list
    set tracked tracked + 1
    set tracked-list replace-item streetId tracked-list tracked
    let visited item streetId visited-list
    set visited visited + 1
    set visited-list replace-item streetId visited-list visited
  ]
end
;Set new destination for the purposive agent
to ResetDestination [who-num]
  let xs [xcor]of goalman who-num
  let ys [ycor]of goalman who-num
  let streetIdF [last-street]of goalman who-num
  let l -1
  ;Purposive walker bahavior
  let streetIdT Purposive_walker_behavior streetIdF
  let streetT item streetIdT streetdata
  let x3 item 2 streetT
  let y3 item 3 streetT
  let x4 item 4 streetT
  let y4 item 5 streetT
  let r1 random-float 1
  let xt (x3 + (x4 - x3)* r1)
  let yt (y3 + (y4 - y3)* r1)
  ;calculate the shortest path  
  let myGoalPaths GetPath xs ys streetIdF xt yt streetIdT
  ;rectify the shortest path by reomoving the trivial segment
  let RectifyGoalPaths (list)
  foreach myGoalPaths
  [
    let path-ele ?
    ifelse is-list? path-ele
    [
      let x1 item 1 path-ele
      let y1 item 2 path-ele
      let x2 item 3 path-ele
      let y2 item 4 path-ele
      
      ifelse (abs(x1 - x2)< 0.00001)and(abs(y1 - y2)< 0.00001)
      [
        ;delete this path segment
      ]
      [
        set RectifyGoalPaths lput path-ele RectifyGoalPaths
      ]
    ]
    [
      set RectifyGoalPaths lput path-ele RectifyGoalPaths
    ]
  ]
  ;ifelse length RectifyGoalPaths > 1
  ifelse length RectifyGoalPaths > 1
  [
    ask goalman who-num
    [
      let firstPathSeg item 1 RectifyGoalPaths
      set cur-street (item 0 firstPathSeg)
      set last-street -1
      set next-street -1
      set cur-length distanceToFrom cur-street (list xcor ycor)
      set junc-length -1
      set junc-point (list)
      set last-location (list)
      set direction 0
      set dest-street streetIdT
      set dest-x xt
      set dest-y yt
      set path-goal RectifyGoalPaths
      set speed simulation-speed
    ]
    
    let tracked item streetIdF tracked-goal-list
    set tracked tracked + 1
    set tracked-goal-list replace-item streetIdF tracked-goal-list tracked
    
    let visited item streetIdT visited-goal-list
    set visited visited + 1
    set visited-goal-list replace-item streetIdT visited-goal-list visited
  ]
  [
    ResetDestination who-num
  ]
end
;Go Once Procedure
to GoOnce
  ifelse MovingBehavior = "Random"
  [
    ask people
    [
      move-agents-random who
    ]
  ]
  [
    ask goalmen
    [ 
      move-agents-purposive who
    ] 
  ]
  tick
  myPlot
end
;Go procedure
to Go
  ifelse MovingBehavior = "Random"
  [
    ask people
    [
      move-agents-random who
    ]
  ]
  [
    ask goalmen
    [ 
      move-agents-purposive who
    ] 
  ]
  tick
  ; to plot the flow every 100 tick
;  let mycount (floor ticks / 100)
;  if mycount = printTime
;  [
;    let tracked-quarter-list (list)
;    ifelse MovingBehavior = "Random"
;    [
;      set tracked-quarter-list map [? ^ 0.25] tracked-list
;    ]
;    [
;      set tracked-quarter-list map [? ^ 0.25] tracked-goal-list
;    ]
;    let rs calculateRS wpr-list tracked-quarter-list
;    set time-series lput printTime (but-first time-series)
;    set wpr-rs-list lput rs wpr-rs-list
;    set printTime printTime + 1
;  ]
  myPlot
end
;Calculate the R Square vlue between two variables
to-report calculateRS [varlist1 varlist2]
  let EvarList1 mean varlist1
  let EvarList2 mean varlist2
  set f length varlist1 
  set g length varlist2
  let crossproduct (map [?1 * ?2] varlist1 varlist2)
  let ECrossProduct mean crossproduct
  let std1 standard-deviation varlist1
  let std2 standard-deviation varlist2
  let myLen length varlist1
  set std1 std1 * sqrt((myLen - 1)/ myLen)
  set std2 std2 * sqrt((myLen - 1)/ myLen) 
  let rs 0
  if std1 != 0 and std2 != 0
  [
    set rs (ECrossProduct - EvarList1 * EvarList2)/(std1 * std2)
    set rs rs * rs
  ]
  report rs
end
;plot the mean velocity, maximum velocity, and minimum velocity
to myPlot
  ;plot the traffic flow distribution
  let trafficFlow-list (list)
  ifelse MovingBehavior = "Random"
  [
    set trafficFlow-list tracked-list  
  ]
  [
    set trafficFlow-list tracked-goal-list
  ]
  ;plot the power law distribution of Traffic flow
  set-current-plot "Power law distribution of Traffic flow"
  clear-plot
  let log-histogram n-values ((max trafficFlow-list) + 1) [0] 
  let min1 (min trafficFlow-list)
  foreach trafficFlow-list 
  [  
    let old-value item ? log-histogram
    set log-histogram replace-item ? log-histogram (old-value + 1)
  ]
  
  let tracked-list-new sort trafficFlow-list
  set tracked-list-new remove-duplicates tracked-list-new
  let tracked-list-new-rectify (list)
  set d length tracked-list-new
  foreach tracked-list-new
  [
    let a-value ?
    set a-value (99 * (a-value - (min tracked-list-new)) / ((max tracked-list-new) - (min tracked-list-new))) + 1
    set tracked-list-new-rectify lput a-value tracked-list-new-rectify
  ]  
  let log-histogram-1 (list)
  foreach log-histogram
  [
     if ? > 0
     [
       set log-histogram-1 lput ? log-histogram-1
     ]
  ]
  let totalNum sum log-histogram-1
  let log-histogram-2 (list)
  foreach log-histogram-1
  [
    let percentage (? / totalNum)
    set log-histogram-2 lput percentage log-histogram-2
  ]
  let index 0
  let log-histogram-rectify (list)
  while [index < length log-histogram-2]
  [
    let index1 index
    let eleSum 0
    while [index1 < length log-histogram-2]
    [
      let ele item index1 log-histogram-2
      set eleSum eleSum + ele
      set index1 index1 + 1
    ]
    set log-histogram-rectify lput eleSum log-histogram-rectify
    set index index + 1
  ]
  ifelse (length log-histogram-rectify) = (length tracked-list-new-rectify)
  [
    set-plot-y-range (precision(log(min log-histogram-rectify)10)2) 0.01    
    (foreach tracked-list-new-rectify log-histogram-rectify
    [
      plotxy (log ?1 10)(log ?2 10)
    ])
  ]
  [
    user-message "error in plot!"
  ]
;  ifelse sliding-plot?
;  [
;    ;plot the R square value of traffic flow and wpr value
;    if not empty? wpr-rs-list
;    [
;      set-current-plot "trafficFlow-vs-Wpr"
;      clear-plot
;      set-plot-pen-mode 2
;      auto-plot-off
;      set-plot-x-range (first time-series) (last time-series)
;      set-plot-y-range 0 1
;      let wpr-rs-list-plot (list)
;      ifelse (length wpr-rs-list)>= 10
;      [
;        set wpr-rs-list-plot sublist wpr-rs-list (printTime - 11) (printTime - 1)
;      ]
;      [
;        foreach wpr-rs-list
;        [
;          set wpr-rs-list-plot lput ? wpr-rs-list-plot
;        ]
;        while [(length wpr-rs-list-plot)< 10]
;        [
;          set wpr-rs-list-plot fput 0 wpr-rs-list-plot
;        ]
;      ]
;      (foreach wpr-rs-list-plot time-series
;      [
;        plotxy ?2 ?1
;      ])
;    ]
;  ]
;  [
;    ;plot the R square value of traffic flow and wpr value
;    if not empty? wpr-rs-list
;    [
;      set-current-plot "trafficFlow-vs-Wpr"
;      clear-plot
;      set-plot-pen-mode 2
;      auto-plot-on
;      let j 1
;      foreach wpr-rs-list
;      [
;        plotxy j ?
;        set j j + 1
;      ]
;    ]
;  ]
  ;plot the R square value of traffic flow and wpr value
  set-current-plot "Correlation between Traffic flow and Metric"
  clear-plot
  set-plot-pen-mode 2
  auto-plot-on
  
  let minFlow (min trafficFlow-list)
  let maxFlow (max trafficFlow-list)
  ifelse MovingBehavior = "Random"
  [ 
    ; let trafficFlow-wpr-list (list)
     let trafficFlow-cnt-list (list)
      ;set c length wlist
      ;set d length trafficFlow-list
    (;foreach trafficFlow-list wpr-list
      ; foreach trafficFlow-list wlist
      foreach trafficFlow-list connect-list
      [
        ;set trafficFlow-wpr-list lput (list ((99 * (?1 - minFlow) / (maxFlow - minFlow)) + 1) ?2) trafficFlow-wpr-list
        if ?1 > 1
        [
         ; set trafficFlow-wpr-list lput (list ?1 ?2) trafficFlow-wpr-list
         set trafficFlow-cnt-list lput (list ?1 ?2) trafficFlow-cnt-list
        ]
      ])
    ; set trafficFlow-wpr-list sort-by [(item 0 ?1)<(item 0 ?2)] trafficFlow-wpr-list
    set trafficFlow-cnt-list sort-by [(item 0 ?1)<(item 0 ?2)] trafficFlow-cnt-list
    ; foreach trafficFlow-wpr-list
    foreach trafficFlow-cnt-list
    [
      plotxy (log(item 0 ?)10) (log(item 1 ?)10)
      ;plotxy ((item 0 ?)^ 0.25) ((item 1 ?)^ 0.25)
    ]
    let trafficFlow-list-quarter map [? ^ 0.25] trafficFlow-list
    ;let wpr-list-quarter map [? ^ 0.25] wpr-list
    ;let wpr-list-quarter map [? ^ 0.25] wlist
    let cnt-list-quarter map [? ^ 0.25] connect-list
    ;fixed precision for number
   ; let rSquare-str (word calculateRS wpr-list-quarter trafficFlow-list-quarter)
    let rSquare-str (word calculateRS cnt-list-quarter trafficFlow-list-quarter)
    while [(length rSquare-str)< 7]
    [
      set rSquare-str (word rSquare-str 0)
    ]
    set rSquare-str (substring rSquare-str 0 7)
    set R-Square rSquare-str
    ;set R-square precision (calculateRS wpr-list-quarter trafficFlow-list-quarter) 5
  ]
  [
         let trafficFlow-cnt-list (list)
    (
      foreach trafficFlow-list connect-list
      [
        if ?1 > 1
        [
         set trafficFlow-cnt-list lput (list ?1 ?2) trafficFlow-cnt-list
        ]
      ])
    set trafficFlow-cnt-list sort-by [(item 0 ?1)<(item 0 ?2)] trafficFlow-cnt-list
    foreach trafficFlow-cnt-list
    [
      plotxy (log(item 0 ?)10) (log(item 1 ?)10)
    ]
    let trafficFlow-list-quarter map [? ^ 0.25] trafficFlow-list
    let cnt-list-quarter map [? ^ 0.25] connect-list
    let rSquare-str (word calculateRS cnt-list-quarter trafficFlow-list-quarter)
    while [(length rSquare-str)< 7]
    [
      set rSquare-str (word rSquare-str 0)
    ]
    set rSquare-str (substring rSquare-str 0 7)
    set R-Square rSquare-str
;     let trafficFlow-ltg-list (list)
;    (
;      foreach trafficFlow-list ltg-list
;      [
;        if ?1 > 1
;        [
;         set trafficFlow-ltg-list lput (list ?1 ?2) trafficFlow-ltg-list
;        ]
;      ])
;    set trafficFlow-ltg-list sort-by [(item 0 ?1)<(item 0 ?2)] trafficFlow-ltg-list
;    foreach trafficFlow-ltg-list
;    [
;      plotxy (log(item 0 ?)10) (log(item 1 ?)10)
;    ]
;    let trafficFlow-list-quarter map [? ^ 0.25] trafficFlow-list
;    let ltg-list-quarter map [? ^ 0.25] ltg-list
;    let rSquare-str (word calculateRS ltg-list-quarter trafficFlow-list-quarter)
;    while [(length rSquare-str)< 7]
;    [
;      set rSquare-str (word rSquare-str 0)
;    ]
;    set rSquare-str (substring rSquare-str 0 7)
;    set R-Square rSquare-str
         
   ; let trafficFlow-betweeness-list (list)
    ;     set a length trafficFlow-list
   ;      set b length blist
  ;  (;foreach trafficFlow-list betweeness-list
   ;   foreach trafficFlow-list blist   
  ;    [
        ;set trafficFlow-wpr-list lput (list ((99 * (?1 - minFlow) / (maxFlow - minFlow)) + 1) ?2) trafficFlow-wpr-list
   ;     if ?1 > 1 and ?2 > 0
    ;    [
       ;   set trafficFlow-betweeness-list lput (list ?1 ?2) trafficFlow-betweeness-list
   ;     ]
   ;   ])
   ; set trafficFlow-betweeness-list sort-by [(item 0 ?1)<(item 0 ?2)] trafficFlow-betweeness-list
  ;  foreach trafficFlow-betweeness-list
  ;  [
  ;    plotxy (log(item 0 ?)10) (log(item 1 ?)10)
      ;plotxy ((item 0 ?)^ 0.25) ((item 1 ?)^ 0.25)
  ;  ]
   ; let trafficFlow-list-quarter map [? ^ 0.25] trafficFlow-list
    ;let betweeness-list-quarter map [? ^ 0.25] betweeness-list
 ;   let betweeness-list-quarter map [? ^ 0.25] blist
    ;fixed precision for number
 ;   let rSquare-str (word calculateRS betweeness-list-quarter trafficFlow-list-quarter)
 ;   while [(length rSquare-str)< 7]
 ;   [
      set rSquare-str (word rSquare-str 0)
  ;  ]
  ;  set rSquare-str (substring rSquare-str 0 7)
  ;  set R-Square rSquare-str
    ;set R-square precision (calculateRS wpr-list-quarter trafficFlow-list-quarter) 5
 ]
end
;Report the distance of a point to the original point on the line
to-report distanceToFrom [streetid pt]
  let streetFeature item streetid streetdata
  let x1 item 2 streetFeature
  let y1 item 3 streetFeature
  let x2 item 0 pt
  let y2 item 1 pt
  let s sqrt((x2 - x1)*(x2 - x1)+(y2 - y1)*(y2 - y1))
  report s
end
;Report the length of a line
to-report distanceToOrig [street id]
  ifelse id != 0
  [
    let x1 item 2 street
    let y1 item 3 street
    let x2 item 4 street
    let y2 item 5 street
    let s sqrt((x2 - x1)*(x2 - x1)+(y2 - y1)*(y2 - y1))
    report s
  ]
  [
    report 0
  ]
end
;Movement of purposive agents
to move-agents-purposive [who-num]
  ask goalman who-num
  [
    ;Decide direction to go
    if direction = 1 ;;from direction
    [
      let street item cur-street streetdata
      ;Get the nearest agent in front of the current agent
      let travelLen speed
      let pLenth cur-length - travelLen
      ifelse pLenth >= junc-length
      [
      ;Walk along one path segment of the shortest path
        let node1 item 0 street
        let node2 item 1 street
        let angle [link-heading] of link node1 node2
        ifelse angle < 180
        [
          set angle angle + 180
        ]
        [
          set angle angle - 180
        ]
        set last-location list xcor ycor
        set heading angle
        fd travelLen
        set cur-length pLenth
        ;Count aggregate flow, footprint
        let tracked item cur-street tracked-goal-list
        set tracked tracked + 1
        set tracked-goal-list replace-item cur-street tracked-goal-list tracked
      ]
      [
      ;Finish current path segment of the shortest path
        ifelse next-street != -1
        [
          ;Choose next path segment as the current path segment
          set last-location list xcor ycor
          setxy item 0 junc-point item 1 junc-point
          let a-street item next-street streetdata
          let x8 item 2 a-street
          let y8 item 3 a-street
          set cur-length sqrt((xcor - x8) * (xcor - x8) + (ycor - y8) * (ycor - y8))
          set last-street cur-street
          set cur-street next-street
          set next-street -1
          set junc-length 0
          set junc-point (list)
          set direction 0  
          ;;Count aggregate flow, footprint
          let tracked item cur-street tracked-goal-list
          set tracked tracked + 1
          set tracked-goal-list replace-item cur-street tracked-goal-list tracked
          let visited item cur-street visited-goal-list
          set visited visited + 1
          set visited-goal-list replace-item cur-street visited-goal-list visited
        ]
        [
          ;Finish the whole path
          set last-location list xcor ycor
          set last-street cur-street
          setxy item 0 junc-point item 1 junc-point
          set cur-length junc-length    
          set junc-point (list)
          set next-street -1
          set junc-length 0
          ;Count aggregate flow, footprint
          let tracked item cur-street tracked-goal-list
          set tracked tracked + 1
          set tracked-goal-list replace-item cur-street tracked-goal-list tracked 
                 
          set direction 0
          set cur-street -1
        ]
      ]
    ]
    if direction = 2   ;to direction
    [
      let street item cur-street streetdata
      let travelLen speed
      let pLenth cur-length + travelLen
      ifelse pLenth <= junc-length
      [
      ;Walk along one path segment of the shortest path
        let node1 item 0 street
        let node2 item 1 street
        let angle [link-heading] of link node1 node2
        set last-location list xcor ycor
        set heading angle
        fd travelLen
        set cur-length pLenth
        ;Count aggregate flow, footprint
        let tracked item cur-street tracked-goal-list
        set tracked tracked + 1
        set tracked-goal-list replace-item cur-street tracked-goal-list tracked
      ]
      [
      ;Finish current path segment of the shortest path
        ifelse next-street != -1
        [
          ;Choose next path segment as the current path segment
          let b-street item next-street streetdata
          let x9 item 2 b-street
          let y9 item 3 b-street
          set last-location list xcor ycor
          setxy item 0 junc-point item 1 junc-point
          set cur-length sqrt((xcor - x9) * (xcor - x9) + (ycor - y9) * (ycor - y9))
          set last-street cur-street
          set cur-street next-street
          set next-street -1
          set junc-length 0
          set junc-point (list)
          set direction 0 
          ;Count aggregate flow, footprint and topological counts
          let tracked item cur-street tracked-goal-list
          set tracked tracked + 1
          set tracked-goal-list replace-item cur-street tracked-goal-list tracked
          let visited item cur-street visited-goal-list
          set visited visited + 1
          set visited-goal-list replace-item cur-street visited-goal-list visited
        ]
        [
          ;Finish the whole path
          set last-location list xcor ycor
          set last-street cur-street
          setxy item 0 junc-point item 1 junc-point
          set cur-length junc-length     
          set junc-point (list)
          set next-street -1
          set junc-length 0
          ;Count aggregate flow, footprint
          let tracked item cur-street tracked-goal-list
          set tracked tracked + 1
          set tracked-goal-list replace-item cur-street tracked-goal-list tracked 
          
          set direction 0
          set cur-street -1
        ]
      ]
    ]
    if direction = 0 ;;no direction
    [
      ;purposive agent reaches the destination
      ifelse (cur-street = -1)and(next-street = -1)
      [
        ;Keep the old destination point as the new start point, and only choose a new destination based on purposive walker behavior 
        ResetDestination who 
        ;Set the red color for the destination street
;        ask link (item 0 (item cur-street streetdata)) (item 1 (item cur-street streetdata)) 
;        [
;          set color white
;        ] 
;        ask destinations
;        [
;          die
;        ] 
;        ask link (item 0 (item dest-street streetdata)) (item 1 (item dest-street streetdata)) 
;        [
;          set color red
;        ]  
;        hatch-destinations 1
;        [
;          setxy ([dest-x]of goalman who-num) ([dest-y]of goalman who-num)
;          set size 2.5
;          set shape "flag"
;          set color blue
;        ]                    
      ]
      ;purposive agent does not reach the destination, set the next path segment
      [
        let lastStreetId item 0 (item ((length path-goal)- 1) path-goal)
        ;Current path segment is the last path segment of the shortest path, Set the next path segment as -1
        ifelse cur-street = lastStreetId
        [
          set next-street -1
          let x2 item 3 (item ((length path-goal)- 1) path-goal)
          let y2 item 4 (item ((length path-goal)- 1) path-goal) 
          set junc-point list x2 y2
          set junc-length distanceToFrom cur-street junc-point
          if cur-length > junc-length
          [
            set direction 1                 ; ;0-no;1-from;2-to
            move-agents-purposive who
          ]
          if cur-length < junc-length
          [
            set direction 2
            move-agents-purposive who
          ]
          if cur-length = junc-length
          [
            set direction 0
            set last-street cur-street
            set cur-street -1
            set next-street -1
            set junc-point (list)
            set junc-length 0
            move-agents-purposive who
          ]
        ]
        ;There exists a next path segment in the shortest path
        [
          ;Find the index of the currentRoad ID in the pathIDs and to set next street, street junction, junction length
          let pathsegments sublist path-goal 1 (length path-goal)
          let curStreetIndexList (list)
          let index 0
          foreach pathsegments
          [
            let pathID item 0 ?
            if pathID = cur-street
            [
              set curStreetIndexList lput index curStreetIndexList
            ]
            set index index + 1
          ]
          let curStreetIndex item 0 curStreetIndexList
          let nextPathSegment item (curStreetIndex + 1)  pathSegments
          set next-street item 0 nextPathSegment
          let x3 item 1 nextPathSegment
          let y3 item 2 nextPathSegment
          set junc-point list x3 y3
          set junc-length distanceToFrom cur-street junc-point
          set cur-length distanceToFrom cur-street (list xcor ycor)
          
          if cur-length > junc-length
          [
            set direction 1                 ;;0:no;1:from;2:to
            move-agents-purposive who
          ]
          if cur-length < junc-length
          [
            set direction 2
            move-agents-purposive who
          ]
          if cur-length = junc-length
          [     
            set direction 0
            set last-street cur-street
            set cur-street next-street
            set next-street -1
            set junc-point (list)
            set junc-length 0
            set cur-length distanceToFrom cur-street (list xcor ycor)
            move-agents-purposive who
          ]
        ]
      ]      
    ]
  ]
end
;Movement of random agents
to move-agents-random [who-num]
  ask person who-num
  [
    let travelLen speed   
    ;Decide direction to go
    if direction = 1 ;;from direction
    [
      let street item cur-street streetdata
      let pLenth cur-length - travelLen
      ifelse pLenth >= junc-length
      [
        ;Walk along the current street towards the next street
        let node1 item 0 street
        let node2 item 1 street
        let angle [link-heading] of link node1 node2
        ifelse angle < 180
        [
          set angle angle + 180
        ]
        [
          set angle angle - 180
        ]
        set last-location list xcor ycor
        set heading angle
        fd travelLen
        set cur-length pLenth
        ;Count aggregate flow, footprint
        let tracked item cur-street tracked-list
        set tracked tracked + 1
        set tracked-list replace-item cur-street tracked-list tracked
      ]
      [
        ;Finish the journey of the current street, and set the next street to the current street
        set last-location list xcor ycor
        setxy item 0 junc-point item 1 junc-point
        let a-street item next-street streetdata
        let x8 item 2 a-street
        let y8 item 3 a-street
        set cur-length sqrt((xcor - x8) * (xcor - x8) + (ycor - y8) * (ycor - y8))
        set last-street cur-street
        set cur-street next-street
        set next-street -1
        set junc-length 0
        set junc-point (list)
        set direction 0 
        ;Count aggregate flow, footprint and topological counts
        let tracked item cur-street tracked-list
        set tracked tracked + 1
        set tracked-list replace-item cur-street tracked-list tracked
        let visited item cur-street visited-list
        set visited visited + 1
        set visited-list replace-item cur-street visited-list visited        
      ]
    ]
    if direction = 2 ;;to direction
    [
      let street item cur-street streetdata
      let pLenth cur-length + travelLen
      ifelse pLenth <= junc-length
      [
        ;Walk along the current street towards the next street
        let node1 item 0 street
        let node2 item 1 street
        let angle [link-heading] of link node1 node2
        set last-location list xcor ycor
        set heading angle
        fd travelLen
        set cur-length pLenth
        ;Count aggregate flow, footprint
        let tracked item cur-street tracked-list
        set tracked tracked + 1
        set tracked-list replace-item cur-street tracked-list tracked
      ]
      [
        ;Finish the journey of the current street, and set the next street to the current street
        let b-street item next-street streetdata
        let x9 item 2 b-street
        let y9 item 3 b-street
        set last-location list xcor ycor
        setxy item 0 junc-point item 1 junc-point
        set cur-length sqrt((xcor - x9) * (xcor - x9) + (ycor - y9) * (ycor - y9))
        set last-street cur-street
        set cur-street next-street
        set next-street -1
        set junc-length 0
        set junc-point (list)
        set direction 0    
        ;Count aggregate flow, footprint and topological counts
        let tracked item cur-street tracked-list
        set tracked tracked + 1
        set tracked-list replace-item cur-street tracked-list tracked
        let visited item cur-street visited-list
        set visited visited + 1
        set visited-list replace-item cur-street visited-list visited
      ]  
    ]
    if direction = 0  ;no direction
    [
      ;Set the next street
      let pDisToFrom cur-length
      let intersects-street item cur-street street-list
      let next-street-list GetNextStreet intersects-street cur-street
      ;;calculate the next-street,junc-point,junc-length and so on.
      set next-street item 0 next-street-list
      ;get the first intersection point,the format is [first-node-id second-node-id x y]
      let first-intersection-point item 1 next-street-list   
      ;let street item cur-street streetdata, and get the coordinates of the first node of the cur-street
      let x1 [xcor] of (node (item 0 first-intersection-point)) 
      let y1 [ycor] of (node (item 0 first-intersection-point))
      let x2 item 2 first-intersection-point
      let y2 item 3 first-intersection-point
      set junc-length sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2))
      set junc-point list x2 y2
      ;Decide the direction of the agent
      if cur-length > junc-length
      [
        set direction 1                 ;;0-no;1-from;2-to
      ]
      if cur-length < junc-length
      [
        set direction 2
      ]
      if cur-length = junc-length
      [
        set direction 0
      ]
      ;Set the red color for the destination street
;      ask link (item 0 (item cur-street streetdata)) (item 1 (item cur-street streetdata)) 
;      [
;        set color white
;      ] 
;      ask link (item 0 (item next-street streetdata)) (item 1 (item next-street streetdata)) 
;      [
;        set color red
;      ]                  
      ifelse direction = 0
      [
        ;the agent return back to the last street
        set last-street cur-street
        set cur-street next-street
        set next-street -1
        set last-location junc-point
        set junc-point (list)
        set junc-length 0
        let x3 [xcor] of (node (item 1 first-intersection-point)) ;;get the xcor of the first node of the cur-street
        let y3 [ycor] of (node (item 1 first-intersection-point))
        set cur-length sqrt((x3 - x2) * (x3 - x2) + (y3 - y2) * (y3 - y2))
        move-agents-random who
      ]
      [
        move-agents-random who
      ]
    ]
  ]
end
;Select the next street from its connected streets randomly with a priority on the most connected street based on the connectivity graph
to-report GetNextStreet [intersects-street pStreet1]
  let pStreet2 item 0 intersects-street
  ifelse pStreet1 = pStreet2
  [
    let intersects-street-1 sublist intersects-street 1 (length intersects-street)
    let connectivitySum 0
    foreach intersects-street-1
    [
      let street-id item 0 ?
      set connectivitySum connectivitySum + length(item street-id street-list) - 1
    ]
    let random-connectivity random connectivitySum
    let cumulCon 0  ;cumulative connectivity
    foreach intersects-street-1
    [
      let street-id item 0 ?
      if ((random-connectivity >= cumulCon) and (random-connectivity <= cumulCon + (length(item street-id street-list) - 1)))
      [
        report ?
      ]
      set cumulCon cumulCon + length(item street-id street-list) - 1
    ]
  ]
  [
    user-message "error in generating the Connectivity Graph!"
    stop 
  ]
end
;Construct the connectivity graph
to ConstructConnectivityGraph
  ifelse (length streetdata) != 0
  [
    let k 0
    set street-list (list)
    foreach streetdata
    [
      let ele (list k)
      set street-list lput ele street-list
      set k k + 1
    ]
    set countAbnormal (list)
    let i 0
    foreach streetdata
    [
      let street1 ?
      let street-list-ele (item i street-list)
      let j 0
      foreach streetdata
      [
        let street2 ? 
        if i < j
        [
          let intersection-list (list j)
          let point Intersection street1 street2
          if not empty? point
          [
            let point1 point
            set point1 fput (item 0 street2) point1
            set point1 fput (item 0 street1) point1
            set intersection-list lput point1 intersection-list
            set street-list-ele lput intersection-list street-list-ele
            
            let street-list-ele-j (item j street-list)
            let point2 point
            set point2 fput (item 0 street1) point2
            set point2 fput (item 0 street2) point2
            let intersection-list-j (list i)
            set intersection-list-j lput point2 intersection-list-j
            set street-list-ele-j lput intersection-list-j street-list-ele-j
            set street-list replace-item j street-list street-list-ele-j
          ]
        ]
        set j j + 1
      ]
      if (length street-list-ele)= 1
      [
        set countAbnormal lput street-list-ele countAbnormal
      ]     
      show street-list-ele
      set street-list replace-item i street-list street-list-ele    
      set i i + 1
    ]
    show street-list
    show countAbnormal
  ]
  [
    user-message "please load street first!"
    stop 
  ]
end
;Get the intersection of two axial line
to-report Intersection [street1 street2]
  let x-s1 item 2 street1
  let y-s1 item 3 street1
  let x-e1 item 4 street1
  let y-e1 item 5 street1
  let x-s2 item 2 street2
  let y-s2 item 3 street2
  let x-e2 item 4 street2
  let y-e2 item 5 street2
  let x1-min 0
  let x1-max 0
  let y1-min 0
  let y1-max 0
  let x2-min 0
  let x2-max 0
  let y2-min 0
  let y2-max 0
  ifelse x-s1 > x-e1
  [ set x1-min x-e1
    set x1-max x-s1
  ]
  [ set x1-min x-s1
    set x1-max x-e1
  ]
  ifelse y-s1 > y-e1
  [ set y1-min y-e1
    set y1-max y-s1
  ]
  [ set y1-min y-s1
    set y1-max y-e1
  ]
  ifelse x-s2 > x-e2
  [ set x2-min x-e2
    set x2-max x-s2
  ]
  [ set x2-min x-s2
    set x2-max x-e2
  ]
  ifelse y-s2 > y-e2
  [ set y2-min y-e2
    set y2-max y-s2
  ]
  [ set y2-min y-s2
    set y2-max y-e2
  ]
  ifelse ((x1-min > x2-max) or (x2-min > x1-max) or (y1-min > y2-max) or (y2-min > y1-max))
  [
    report []
  ]
  [
    let m1 0
    let m2 0
    if x-s1 = x-e1
    [
      ifelse x-s2 = x-e2
      [report [] ]
      [
        report Intersection street2 street1
      ]
    ]
    if x-s2 = x-e2
    [
      set m1 (y-e1 - y-s1) / (x-e1 - x-s1)
      let c1 (y-s1 - m1 * x-s1)
      let x x-s2
      let y m1 * x + c1
      if not x-within? x x-s1 x-e1 [ report [] ]
      if not y-within? y y-s2 y-e2 [ report [] ]
      report list x y
    ]
    set m2 (y-e2 - y-s2) / (x-e2 - x-s2)
    set m1 (y-e1 - y-s1) / (x-e1 - x-s1)
    if m1 = m2
    [report []]
    let c1 (y-s1 - m1 * x-s1)
    let c2 (y-s2 - m2 * x-s2)
    let x (c2 - c1) / (m1 - m2)
    ;; check if intersection point lies on both segments
    if not x-within? x x-s1 x-e1 [ report [] ]
    if not x-within? x x-s2 x-e2 [ report [] ]
    report list x (m1 * x + c1)
  ]
end
;To judge if x lies within the scope of x1 and x2
to-report x-within? [x x1 x2]  
  let w abs(x1 - x2)
  let mid (x1 + x2) / 2
  set w w / 2
  report abs (mid - x) <= w
end
;To judge if y lies within the scope of y1 and y2
to-report y-within? [y y1 y2]  
  let w abs(y1 - y2)
  let mid (y1 + y2) / 2
  set w w / 2
  report abs (mid - y) <= w
end
;Compute the shortest distance path between the current location and destination
;report the data format like [0,[streetID1,x1,y1,x1,y1]]  VS  [1,[streetID1,x1,y1,x2,y2],[streetID2,x1,y1,x2,y2],[streetID3,x1,y1,x2,y2]] NOTE,1 indicates that there is at least one path, But 0 indicates that the OD point are identical, So there exists no path.
to-report GetPath[x1 y1 street1 x2 y2 street2]  
  if street1 = street2
  [
    let lineInfo sudLine street1 x1 y1 x2 y2
    report lineInfo
  ]
  let topoDist 0
  let geoDist 1000000000
  let flag TopoShortestPath street1 street2
  ifelse flag = true
  [
    let currPathDist 0
    let minPathIdx -1
    let i 0
    foreach pathIDsList
    [
      set currPathDist pathDist ? x1 y1 street1 x2 y2 street2
      if currPathDist < geoDist
      [
        set geoDist currPathDist
        set minPathIdx i
      ]
      set i i + 1
    ]
    let pathStr item minPathIdx pathIDsList
    let myResult pathDistOutput pathStr x1 y1 street1 x2 y2 street2
    set myResult fput 1 myResult
    report myResult
  ]
  [
     user-message "Exist Isolation in the StreetNetwork!"
     stop 
  ]
end
to-report pathDistOutput [pathStr x1 y1 street1 x2 y2 street2]
  let lineInfo (list)
  
  let currPt (list x1 y1)
  let nxtPt (list)
  let currFID street1
  let nxtFID -1
  let commaIdx position "," pathStr
  set pathStr substring pathStr (commaIdx) (length pathStr)
  
  while [(position "," pathStr) != false]
  [
    set pathStr remove-item 0 pathStr
    set commaIdx position "," pathStr
    ifelse commaIdx = false
    [
      set nxtFID read-from-string pathStr
    ]
    [
      set nxtFID read-from-string (substring pathStr 0 commaIdx)
    ]
    let streetF item currFID streetdata
    let streetT item nxtFID streetdata
    set nxtPt Intersection streetF streetT
    
    set lineInfo lput (list currFID (item 0 currPt) (item 1 currPt) (item 0 nxtPt) (item 1 nxtPt)) lineInfo
    
    set currFID nxtFID
    set currPt nxtPt
    if commaIdx != false
    [
      set pathStr substring pathStr commaIdx (length pathStr)
    ]
  ]
  set lineInfo lput (list nxtFID (item 0 nxtPt) (item 1 nxtPt) x2 y2) lineInfo
  report lineInfo
end
to-report pathDist [pathStr x1 y1 street1 x2 y2 street2]
    
  let currPt (list x1 y1)
  let nxtPt (list)
  let currFID street1
  let nxtFID -1
  let commaIdx position "," pathStr
  set pathStr substring pathStr (commaIdx) (length pathStr)
  
  let rtnDist 0
  while [(position "," pathStr) != false]
  [
    set pathStr remove-item 0 pathStr
    set commaIdx position "," pathStr
    ifelse commaIdx = false
    [
      set nxtFID read-from-string pathStr
    ]
    [
      set nxtFID read-from-string (substring pathStr 0 commaIdx)
    ]
    let streetF item currFID streetdata
    let streetT item nxtFID streetdata
    set nxtPt Intersection streetF streetT
    
    let mydist GetMyDist currPt nxtPt
    set rtnDist (rtnDist + mydist)
    
    set currFID nxtFID
    set currPt nxtPt
    if commaIdx != false
    [
      set pathStr substring pathStr commaIdx (length pathStr)
    ]
  ]
  let mydist1 GetMyDist nxtPt (list x2 y2)
  set rtnDist (rtnDist + mydist1)
  report rtnDist
end
to-report GetMyDist [pt1 pt2]
  let x1 item 0 pt1
  let y1 item 1 pt1
  let x2 item 0 pt2
  let y2 item 1 pt2
  
  report sqrt((x1 - x2)*(x1 - x2)+(y1 - y2)*(y1 - y2))
end
to-report TopoShortestPath [streetO streetD]
  set pathList (list)
  set pathIDsList (list)
  TopoSteps streetO
  let topoDist item streetD currLineDepth
  set degree item streetD currLineDepth
  if degree = 0
  [
    report false
  ]
  if degree = 1
  [
    if isConnect? streetO streetD
    [
      let pathString (word streetO "," streetD)
      set pathIDsList lput pathString pathIDsList
    ]
    report true
  ]
  let j 0
  while [j < degree - 1]
  [
    set pathList lput (list) pathList  ;pathList [[],[],[],...]
    set j j + 1
  ]
  let i 0
  set j 1
  let numFeatures length currLineDepth
  while [i < numFeatures]
  [
    let curDegree item i currLineDepth
    if curDegree < degree
    [
      set j 1
      while [j < degree]
      [
        if curDegree = j
        [
          let pathList-ele item (j - 1) pathList
          set pathList-ele lput i pathList-ele
          set pathList replace-item (j - 1) pathList pathList-ele
        ]
        set j j + 1
      ]
    ]
    set i i + 1
  ]
  
  let validPathIDList (list)
  set validPathIDList lput streetD validPathIDList
  filterPath validPathIDList
  
  set currPath (word streetO ",")
  set currDegree 0
  let localDegree item streetD currLineDepth
  buildPath streetO streetD localDegree
  
  report true
end
to buildPath [currFID endFID localDegree]
  let currDegreeList item currDegree pathList
  foreach currDegreeList
  [
    let nxtFID ?
    if isConnect? currFID nxtFID
    [
      set currPath (word currPath nxtFID ",")
      ifelse currDegree < (localDegree - 2)
      [
        set currDegree currDegree + 1
        buildPath nxtFID endFID localDegree
      ]
      [
        set pathIDsList lput (word currPath endFID) pathIDsList
      ]
      set currPath remove-item ((length currPath)- 1) currPath
      let reverseCurrPath reverse currPath
      let lastCommaIndex position "," reverseCurrPath
      let lastPosition (length currPath) - lastCommaIndex
      set currPath substring currPath 0 lastPosition 
    ]
  ]
  set currDegree currDegree - 1
end
to filterPath [validPathIDList]
  let tempDegreeList item (degree - 2) pathList
  let tmpValidList (list)
  foreach validPathIDList
  [
    let validPathId ?
    let currFeatConn item validPathId street-list
    set currFeatConn sublist currFeatConn 1 (length currFeatConn)
    foreach currFeatConn
    [
      let mID item 0 ?
      if member? mID tempDegreeList
      [
        if not member? mID tmpValidList
        [
          set tmpValidList lput mID tmpValidList
        ]
      ]
    ]
  ]
  set pathList replace-item (degree - 2) pathList tmpValidList
  set degree degree - 1
  if degree >= 2
  [
    filterPath tmpValidList
  ]
end
to-report isConnect? [streetId1 streetId2]
  let street-adjacent item streetId1 street-list
  set street-adjacent sublist street-adjacent 1 (length street-adjacent)
  foreach street-adjacent
  [
    let a-street ?
    let a-street-id item 0 a-street
    if a-street-id = streetId2
    [
      report true
    ]
  ]
  report false
end
to TopoSteps [streetId]
  set currLineDepth (list)
  let i 0
  while [i < maxBound]
  [
    set currLineDepth lput 0 currLineDepth  ; Initialize the currLineDepth
    set i i + 1
  ]
  set currLevel 0
  let localPoints1 (list)
  
  let street-adjacent item streetId street-list
  set street-adjacent sublist street-adjacent 1 (length street-adjacent)
  foreach street-adjacent
  [
    let a-street ?
    let a-street-id item 0 a-street
    set localPoints1 lput a-street-id localPoints1    
  ]
  
  let sndj 0
  foreach street-adjacent
  [
    let a-street ?
    let a-street-id item 0 a-street
    set sndj a-street-id
    set currLineDepth replace-item sndj currLineDepth 1
  ]
  
  set currLevel 2
  recurDegree streetId localPoints1
end
to recurDegree [currLineNo localPoints1]
  let connj 0
  let recurConnPoints (list)
  foreach localPoints1
  [
    set connj ?
    let rdj 0
    let street-list-ele item connj street-list
    set street-list-ele sublist street-list-ele 1 (length street-list-ele)
    foreach street-list-ele
    [
      set rdj item 0 ?
      ifelse rdj = currLineNo
      []    ;???????
      [
        let a-index item rdj currLineDepth
        if a-index = 0
        [
          set currLineDepth replace-item rdj currLineDepth currLevel
          set recurConnPoints lput rdj recurConnPoints
        ]
      ]
    ]
  ]
  if (length recurConnPoints)> 0
  [
    set currLevel currLevel + 1
    recurDegree currLineNo recurConnPoints
  ]
  set recurConnPoints (list)
end
to-report sudLine [streetId x1 y1 x2 y2]
  ifelse (abs(x1 - x2) < simulation-speed) and (abs(y1 - y2) < simulation-speed)
  [
    let lineInfo (list 0)
    set lineInfo lput (list streetId x1 y1 x2 y2) lineInfo
    report lineInfo
  ]
  [
    let lineInfo (list 1)
    set lineInfo lput (list streetId x1 y1 x2 y2) lineInfo
    report lineInfo
  ]
end
@#$#@#$#@
GRAPHICS-WINDOW
213
13
615
436
24
24
8.0
1
10
1
1
1
0
0
0
1
-24
24
-24
24
0
0
1
ticks
30.0

BUTTON
67
148
162
181
Setup
Setup
NIL
1
T
OBSERVER
NIL
NIL
NIL
NIL
1

BUTTON
67
206
162
239
NIL
Go
T
1
T
OBSERVER
NIL
NIL
NIL
NIL
1

SLIDER
28
287
196
320
simulation-speed
simulation-speed
0
0.1
0.07
0.01
1
pixel/s
HORIZONTAL

CHOOSER
67
75
161
120
MovingBehavior
MovingBehavior
"Random" "Purposive I" "Purposive II"
1

TEXTBOX
51
30
201
72
After selecting moving behavior, click SETUP, then GO
11
0.0
1

SLIDER
31
365
201
398
car-number
car-number
0
200
123
1
1
NIL
HORIZONTAL

PLOT
630
230
957
434
Power law distribution of Traffic flow
Log traffic-flow
Log traffic-frequency
0.0
0.0
0.0
0.0
true
false
"" ""
PENS
"default" 1.0 0 -16777216 true "" ""

PLOT
629
13
957
219
Correlation between Traffic flow and Metric
Log traffic-flow
Log Metric
0.0
0.0
0.0
0.0
true
false
"" ""
PENS
"default" 1.0 0 -16777216 true "" ""

MONITOR
879
154
948
199
R-square:
R-square
17
1
11

@#$#@#$#@
## WHAT IS IT?

This section could give a general understanding of what the model is trying to show or explain.

## HOW IT WORKS

This section could explain what rules the agents use to create the overall behavior of the model.

## HOW TO USE IT

This section could explain how to use the model, including a description of each of the items in the interface tab.

## THINGS TO NOTICE

This section could give some ideas of things for the user to notice while running the model.

## THINGS TO TRY

This section could give some ideas of things for the user to try to do (move sliders, switches, etc.) with the model.

## EXTENDING THE MODEL

This section could give some ideas of things to add or change in the procedures tab to make the model more complicated, detailed, accurate, etc.

## NETLOGO FEATURES

This section could point out any especially interesting or unusual features of NetLogo that the model makes use of, particularly in the Procedures tab.  It might also point out places where workarounds were needed because of missing features.

## RELATED MODELS

This section could give the names of models in the NetLogo Models Library or elsewhere which are of related interest.

## CREDITS AND REFERENCES

This section could contain a reference to the model's URL on the web if it has one, as well as any other necessary credits or references.
@#$#@#$#@
default
true
0
Polygon -7500403 true true 150 5 40 250 150 205 260 250

airplane
true
0
Polygon -7500403 true true 150 0 135 15 120 60 120 105 15 165 15 195 120 180 135 240 105 270 120 285 150 270 180 285 210 270 165 240 180 180 285 195 285 165 180 105 180 60 165 15

arrow
true
0
Polygon -7500403 true true 150 0 0 150 105 150 105 293 195 293 195 150 300 150

box
false
0
Polygon -7500403 true true 150 285 285 225 285 75 150 135
Polygon -7500403 true true 150 135 15 75 150 15 285 75
Polygon -7500403 true true 15 75 15 225 150 285 150 135
Line -16777216 false 150 285 150 135
Line -16777216 false 150 135 15 75
Line -16777216 false 150 135 285 75

bug
true
0
Circle -7500403 true true 96 182 108
Circle -7500403 true true 110 127 80
Circle -7500403 true true 110 75 80
Line -7500403 true 150 100 80 30
Line -7500403 true 150 100 220 30

butterfly
true
0
Polygon -7500403 true true 150 165 209 199 225 225 225 255 195 270 165 255 150 240
Polygon -7500403 true true 150 165 89 198 75 225 75 255 105 270 135 255 150 240
Polygon -7500403 true true 139 148 100 105 55 90 25 90 10 105 10 135 25 180 40 195 85 194 139 163
Polygon -7500403 true true 162 150 200 105 245 90 275 90 290 105 290 135 275 180 260 195 215 195 162 165
Polygon -16777216 true false 150 255 135 225 120 150 135 120 150 105 165 120 180 150 165 225
Circle -16777216 true false 135 90 30
Line -16777216 false 150 105 195 60
Line -16777216 false 150 105 105 60

car
false
0
Polygon -7500403 true true 300 180 279 164 261 144 240 135 226 132 213 106 203 84 185 63 159 50 135 50 75 60 0 150 0 165 0 225 300 225 300 180
Circle -16777216 true false 180 180 90
Circle -16777216 true false 30 180 90
Polygon -16777216 true false 162 80 132 78 134 135 209 135 194 105 189 96 180 89
Circle -7500403 true true 47 195 58
Circle -7500403 true true 195 195 58

car top
true
0
Polygon -7500403 true true 151 8 119 10 98 25 86 48 82 225 90 270 105 289 150 294 195 291 210 270 219 225 214 47 201 24 181 11
Polygon -16777216 true false 210 195 195 210 195 135 210 105
Polygon -16777216 true false 105 255 120 270 180 270 195 255 195 225 105 225
Polygon -16777216 true false 90 195 105 210 105 135 90 105
Polygon -1 true false 205 29 180 30 181 11
Line -7500403 false 210 165 195 165
Line -7500403 false 90 165 105 165
Polygon -16777216 true false 121 135 180 134 204 97 182 89 153 85 120 89 98 97
Line -16777216 false 210 90 195 30
Line -16777216 false 90 90 105 30
Polygon -1 true false 95 29 120 30 119 11

circle
false
0
Circle -7500403 true true 0 0 300

circle 2
false
0
Circle -7500403 true true 0 0 300
Circle -16777216 true false 30 30 240

cow
false
0
Polygon -7500403 true true 200 193 197 249 179 249 177 196 166 187 140 189 93 191 78 179 72 211 49 209 48 181 37 149 25 120 25 89 45 72 103 84 179 75 198 76 252 64 272 81 293 103 285 121 255 121 242 118 224 167
Polygon -7500403 true true 73 210 86 251 62 249 48 208
Polygon -7500403 true true 25 114 16 195 9 204 23 213 25 200 39 123

cylinder
false
0
Circle -7500403 true true 0 0 300

dot
false
0
Circle -7500403 true true 90 90 120

face happy
false
0
Circle -7500403 true true 8 8 285
Circle -16777216 true false 60 75 60
Circle -16777216 true false 180 75 60
Polygon -16777216 true false 150 255 90 239 62 213 47 191 67 179 90 203 109 218 150 225 192 218 210 203 227 181 251 194 236 217 212 240

face neutral
false
0
Circle -7500403 true true 8 7 285
Circle -16777216 true false 60 75 60
Circle -16777216 true false 180 75 60
Rectangle -16777216 true false 60 195 240 225

face sad
false
0
Circle -7500403 true true 8 8 285
Circle -16777216 true false 60 75 60
Circle -16777216 true false 180 75 60
Polygon -16777216 true false 150 168 90 184 62 210 47 232 67 244 90 220 109 205 150 198 192 205 210 220 227 242 251 229 236 206 212 183

fish
false
0
Polygon -1 true false 44 131 21 87 15 86 0 120 15 150 0 180 13 214 20 212 45 166
Polygon -1 true false 135 195 119 235 95 218 76 210 46 204 60 165
Polygon -1 true false 75 45 83 77 71 103 86 114 166 78 135 60
Polygon -7500403 true true 30 136 151 77 226 81 280 119 292 146 292 160 287 170 270 195 195 210 151 212 30 166
Circle -16777216 true false 215 106 30

flag
false
0
Rectangle -7500403 true true 60 15 75 300
Polygon -7500403 true true 90 150 270 90 90 30
Line -7500403 true 75 135 90 135
Line -7500403 true 75 45 90 45

flower
false
0
Polygon -10899396 true false 135 120 165 165 180 210 180 240 150 300 165 300 195 240 195 195 165 135
Circle -7500403 true true 85 132 38
Circle -7500403 true true 130 147 38
Circle -7500403 true true 192 85 38
Circle -7500403 true true 85 40 38
Circle -7500403 true true 177 40 38
Circle -7500403 true true 177 132 38
Circle -7500403 true true 70 85 38
Circle -7500403 true true 130 25 38
Circle -7500403 true true 96 51 108
Circle -16777216 true false 113 68 74
Polygon -10899396 true false 189 233 219 188 249 173 279 188 234 218
Polygon -10899396 true false 180 255 150 210 105 210 75 240 135 240

house
false
0
Rectangle -7500403 true true 45 120 255 285
Rectangle -16777216 true false 120 210 180 285
Polygon -7500403 true true 15 120 150 15 285 120
Line -16777216 false 30 120 270 120

leaf
false
0
Polygon -7500403 true true 150 210 135 195 120 210 60 210 30 195 60 180 60 165 15 135 30 120 15 105 40 104 45 90 60 90 90 105 105 120 120 120 105 60 120 60 135 30 150 15 165 30 180 60 195 60 180 120 195 120 210 105 240 90 255 90 263 104 285 105 270 120 285 135 240 165 240 180 270 195 240 210 180 210 165 195
Polygon -7500403 true true 135 195 135 240 120 255 105 255 105 285 135 285 165 240 165 195

line
true
0
Line -7500403 true 150 0 150 300

line half
true
0
Line -7500403 true 150 0 150 150

pentagon
false
0
Polygon -7500403 true true 150 15 15 120 60 285 240 285 285 120

person
false
0
Circle -7500403 true true 110 5 80
Polygon -7500403 true true 105 90 120 195 90 285 105 300 135 300 150 225 165 300 195 300 210 285 180 195 195 90
Rectangle -7500403 true true 127 79 172 94
Polygon -7500403 true true 195 90 240 150 225 180 165 105
Polygon -7500403 true true 105 90 60 150 75 180 135 105

plant
false
0
Rectangle -7500403 true true 135 90 165 300
Polygon -7500403 true true 135 255 90 210 45 195 75 255 135 285
Polygon -7500403 true true 165 255 210 210 255 195 225 255 165 285
Polygon -7500403 true true 135 180 90 135 45 120 75 180 135 210
Polygon -7500403 true true 165 180 165 210 225 180 255 120 210 135
Polygon -7500403 true true 135 105 90 60 45 45 75 105 135 135
Polygon -7500403 true true 165 105 165 135 225 105 255 45 210 60
Polygon -7500403 true true 135 90 120 45 150 15 180 45 165 90

square
false
0
Rectangle -7500403 true true 30 30 270 270

square 2
false
0
Rectangle -7500403 true true 30 30 270 270
Rectangle -16777216 true false 60 60 240 240

squirrel
false
0
Polygon -7500403 true true 87 267 106 290 145 292 157 288 175 292 209 292 207 281 190 276 174 277 156 271 154 261 157 245 151 230 156 221 171 209 214 165 231 171 239 171 263 154 281 137 294 136 297 126 295 119 279 117 241 145 242 128 262 132 282 124 288 108 269 88 247 73 226 72 213 76 208 88 190 112 151 107 119 117 84 139 61 175 57 210 65 231 79 253 65 243 46 187 49 157 82 109 115 93 146 83 202 49 231 13 181 12 142 6 95 30 50 39 12 96 0 162 23 250 68 275
Polygon -16777216 true false 237 85 249 84 255 92 246 95
Line -16777216 false 221 82 213 93
Line -16777216 false 253 119 266 124
Line -16777216 false 278 110 278 116
Line -16777216 false 149 229 135 211
Line -16777216 false 134 211 115 207
Line -16777216 false 117 207 106 211
Line -16777216 false 91 268 131 290
Line -16777216 false 220 82 213 79
Line -16777216 false 286 126 294 128
Line -16777216 false 193 284 206 285

star
false
0
Polygon -7500403 true true 151 1 185 108 298 108 207 175 242 282 151 216 59 282 94 175 3 108 116 108

target
false
0
Circle -7500403 true true 0 0 300
Circle -16777216 true false 30 30 240
Circle -7500403 true true 60 60 180
Circle -16777216 true false 90 90 120
Circle -7500403 true true 120 120 60

tree
false
0
Circle -7500403 true true 118 3 94
Rectangle -6459832 true false 120 195 180 300
Circle -7500403 true true 65 21 108
Circle -7500403 true true 116 41 127
Circle -7500403 true true 45 90 120
Circle -7500403 true true 104 74 152

triangle
false
0
Polygon -7500403 true true 150 30 15 255 285 255

triangle 2
false
0
Polygon -7500403 true true 150 30 15 255 285 255
Polygon -16777216 true false 151 99 225 223 75 224

truck
false
0
Rectangle -7500403 true true 4 45 195 187
Polygon -7500403 true true 296 193 296 150 259 134 244 104 208 104 207 194
Rectangle -1 true false 195 60 195 105
Polygon -16777216 true false 238 112 252 141 219 141 218 112
Circle -16777216 true false 234 174 42
Rectangle -7500403 true true 181 185 214 194
Circle -16777216 true false 144 174 42
Circle -16777216 true false 24 174 42
Circle -7500403 false true 24 174 42
Circle -7500403 false true 144 174 42
Circle -7500403 false true 234 174 42

turtle
true
0
Polygon -10899396 true false 215 204 240 233 246 254 228 266 215 252 193 210
Polygon -10899396 true false 195 90 225 75 245 75 260 89 269 108 261 124 240 105 225 105 210 105
Polygon -10899396 true false 105 90 75 75 55 75 40 89 31 108 39 124 60 105 75 105 90 105
Polygon -10899396 true false 132 85 134 64 107 51 108 17 150 2 192 18 192 52 169 65 172 87
Polygon -10899396 true false 85 204 60 233 54 254 72 266 85 252 107 210
Polygon -7500403 true true 119 75 179 75 209 101 224 135 220 225 175 261 128 261 81 224 74 135 88 99

wheel
false
0
Circle -7500403 true true 3 3 294
Circle -16777216 true false 30 30 240
Line -7500403 true 150 285 150 15
Line -7500403 true 15 150 285 150
Circle -7500403 true true 120 120 60
Line -7500403 true 216 40 79 269
Line -7500403 true 40 84 269 221
Line -7500403 true 40 216 269 79
Line -7500403 true 84 40 221 269

x
false
0
Polygon -7500403 true true 270 75 225 30 30 225 75 270
Polygon -7500403 true true 30 75 75 30 270 225 225 270

@#$#@#$#@
NetLogo 5.0.3
@#$#@#$#@
@#$#@#$#@
@#$#@#$#@
@#$#@#$#@
@#$#@#$#@
default
0.0
-0.2 0 0.0 1.0
0.0 1 1.0 0.0
0.2 0 0.0 1.0
link direction
true
0
Line -7500403 true 150 150 90 180
Line -7500403 true 150 150 210 180

@#$#@#$#@
0
@#$#@#$#@
